package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.DoctorCharge;
import com.naiterui.ehp.bp.domain.DrChargeConfig;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.dao.IDoctorChargeDao;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.dao.IDrChargeConfigDao;
import com.naiterui.ehp.bs.doctor.service.IDoctorChargeService;
import com.naiterui.ehp.bs.doctor.service.IDrSerItemsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 收费标准业务接口实现
 *
 * @author Amber
 * @since 1.0.0
 */
@Service
public class AdDoctorChargeServiceImpl implements IDoctorChargeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorChargeServiceImpl.class);

    @Autowired
    IDoctorChargeDao doctorChargeDao;
    @Autowired
    IDrChargeConfigDao drChargeConfigDao;
    @Autowired
    IDoctorDao doctorDao;
    @Autowired
    IDrSerItemsService drSerItemsService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveDefaultCharge(Doctor doctor, int type) throws BusinessException {

        // 获取指定类型服务收费配置数据
        DrChargeConfig chargeConfig = this.drChargeConfigDao.getChargeConfigByTypeAndTitle(type, doctor.getTitleId() == null ? -1 :
                doctor.getTitleId());
        if (chargeConfig == null || chargeConfig.getDefaultCharge() == 0) {
            LOGGER.warn("charge config is null or zero doctorId {},  type {}", doctor, type);
            return;
        }

        // 设置医生收费信息
        DoctorCharge doctorCharge = this.doctorChargeDao.getDoctorChargeByType(doctor.getId(), type);

        if (doctorCharge == null) {
            // 如果没有收费记录，初始化收费记录,首次开启
            doctorCharge = new DoctorCharge(doctor.getId(), type, chargeConfig.getDefaultCharge(), chargeConfig.getChargePeriod());
        } else {
            // 只是修改费用信息
            doctorCharge.setCharge(chargeConfig.getDefaultCharge());
        }
        // 保存收费信息记录
        this.doctorChargeDao.saveOrUpdate(doctorCharge);
    }
}
